﻿import ArgumentInfo = require('entities/argumentinfo');

import _system = require('durandal/system');
import _app = require('durandal/app');
import _ko = require('knockout');

export class ArgumentEdit {
    private isNewArgument: boolean = false;

    public currentArgument: KnockoutObservable<ArgumentInfo> = null;

    constructor() { }

    public displayName: string = 'ArgumentEdit';

    public canActivate(): boolean {
        return true;
    }

    public activate(argument: ArgumentInfo): boolean {
        if (!argument) {
            this.isNewArgument = true;
            //argument = new ArgumentInfo();
        }
        this.currentArgument = _ko.observable<ArgumentInfo>(argument);

        return true;
    }


    public save(): any {
        var argument: ArgumentInfo = this.currentArgument();
        var title: string = argument.title.tempValue();
        if (!title || (argument.getIdentityDisplayString() == title)) {
            _app.showMessage('Invalid title! Please enter a descriptive title for this argument.', 'Input Validation');
            return false;
        }

        argument.commit();
        return _app.closeDialog(this, argument);
    }

    public cancel(): any {
        this.currentArgument().reset();
        return _app.closeDialog(this);
    }
}

return new ArgumentEdit();